package asia.doing.service.front;


import asia.doing.entity.Article;
import asia.doing.entity.User;
import asia.doing.mapper.ArticleMapper;
import asia.doing.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

@Service(value = "frontUserService")
public class UserService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private ArticleMapper articleMapper;

    public List<User> selectUser(User user) {
        Article article = new Article();
        article.setUserId(user.getId());
        List<Article> articles = articleMapper.selectAll(article);
        List<Integer> articlesIds = new ArrayList<>();
        articles.forEach(article1 -> {
            articlesIds.add(article1.getId());
        });
        List<User> users = userMapper.selectAll(user);
        Integer[] articleIdsArray = articlesIds.toArray(new Integer[articlesIds.size()]);
        users.get(0).setArticles(articleIdsArray);
        return users;
    }

    public PageInfo<User> selectPage(User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        //注意:这里获取到的信息是重复的
        List<User> list = userMapper.selectAll(user);
        System.out.println(list.size());
        PageInfo<User> pageInfo = PageInfo.of(list);
//      List<Activity> activityList = pageInfo.getList(); 可以获取所有列表循环做其他事情
        return pageInfo;
    }
}
